/**
 * 
 */
package com.worksmap.core.action.rate;

import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.worksmap.core.action.BaseController;
import com.worksmap.core.entity.TaoBaoSeller;
import com.worksmap.core.exception.WebUtilException;
import com.worksmap.core.service.LoginService;
import com.worksmap.core.service.RateService;
import com.worksmap.core.service.TradeService;
import com.worksmap.core.service.UserService;
import com.worksmap.core.util.StringUtil;

/**
 * @author jack
 * 2013.12.17
 */
@Controller
@RequestMapping("/rate")
public class RateController extends BaseController {
	
	@Autowired
	UserService userService;
	@Autowired
	RateService rateService;
	@Autowired
	LoginService loginService;
	@Autowired
	TradeService tradeService;
	
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String index(HttpServletRequest request, HttpServletResponse reponse){
		TaoBaoSeller seller = userService.getCurrentSeller(request);
		request.setAttribute("userName", seller.getTaobao_user_nick());
		logger.info("seller: " + seller);
		return "rate/index";
	}
	
	/**
	 * 访问http://127.0.0.1/rate/callBack 
	 * 平台回调地址(正式使用)
	 * jack
	 * 2013-7-5
	 */
	@RequestMapping(value = "callBack", method = RequestMethod.GET)
	public Object callBack(HttpServletRequest request, HttpServletResponse reponse){
		String code = request.getParameter("code");
		String state = request.getParameter("state");
		if(StringUtil.isEmpty(code)){
			request.setAttribute("error", request.getParameter("error"));
			request.setAttribute("errorDescription", request.getParameter("error_description"));
			logger.info("非正常回调");
			return "rate/error";
		}
		logger.info("平台回调" );
		logger.info("code:" + code + ",state:" + state);
		try{
			userService.getSellerInfo(code, state, reponse);
		}catch(WebUtilException e){
			request.setAttribute("errorDescription", e.getMessage());
			return "rate/error";
		}
		return new ModelAndView(new RedirectView(""));
	}
	
	/**
	 * 访问http://127.0.0.1/rate/callBack 
	 * 平台回调地址(沙箱测试)
	 * 到topApp设置为沙箱
	 * 1.到应用后台的沙箱环境管理中，授权用户列表添加欲授权用户nick
	 * 2.访问https://oauth.tbsandbox.com/authorize?response_type=code&client_id=1021706417
				&redirect_uri=http://worksmap.com/rate/callBackSandBox/&state=&view=web
	 * 3.回调该方法获取用户sessionkey
	 * jack
	 * 2013-7-5
	 */
	@RequestMapping(value = "callBackSandBox", method = RequestMethod.GET)
	public Object callBackSandBox(HttpServletRequest request, HttpServletResponse reponse){
		String code = request.getParameter("code");
		String state = request.getParameter("state");
		if(StringUtil.isEmpty(code)){
			TaoBaoSeller seller = userService.getCurrentSeller(request);
			request.setAttribute("userName", seller.getTaobao_user_nick());
			logger.info("seller: " + seller);
			return "rate/index";
		}
		logger.info("平台回调" );
		logger.info("code:" + code + ",state:" + state);
		try{
			userService.getSellerInfo(code, state, reponse);
		}catch(WebUtilException e){
			request.setAttribute("errorDescription", e.getMessage());
			return "rate/error";
		}
		return new ModelAndView(new RedirectView(""));
	}
	
	@RequestMapping(value = "rate", method = RequestMethod.GET)
	public Object rateAllOrder(HttpServletRequest request, HttpServletResponse reponse) throws ParseException{
//		String startTime = request.getParameter("startTime");
//		String endTime = request.getParameter("endTime");
		String startTime = "2013-10-19 00:00:00";
		String endTime = "2013-12-27 23:59:00";
//		TaoBaoSeller seller = new TaoBaoSeller();
//		seller.setAccess_token(RateConstant.sessionKey);
		String code = request.getParameter("code");
		String state = request.getParameter("state");
		if(StringUtil.isEmpty(code)){
			request.setAttribute("error", request.getParameter("error"));
			request.setAttribute("errorDescription", request.getParameter("error_description"));
			return "rate/error";
		}
		TaoBaoSeller seller = userService.getSellerInfo(code, state, reponse);
		rateService.rateOrder(seller, startTime, endTime);
		return "rate/index";
	}
	
}
